package org.komarichin.services;

import org.komarichin.beans.IArticle;
import org.komarichin.beans.ILanguage;
import org.komarichin.beans.dao.IArticleDAO;
import org.komarichin.beans.service.IArticleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;

/**
 * <p>
 * &nbsp;
 * </p>
 * User: Michael Komarichin
 * Date: Jan 23, 2010
 * Time: 10:36:09 PM
 */
@Service
public class ArticleService<PK> implements IArticleService<PK> {


    private static final Logger log = LoggerFactory.getLogger(ArticleService.class);
    private static final long serialVersionUID = -3854104760544834681L;

    public ArticleService() {
        log.debug("instance of article service created.");
    }

    @Autowired
    private IArticleDAO<IArticle<PK>, PK> articleDAO;

    @Override
    public IArticle<PK> save(IArticle<PK> article) {
        if (article == null) {
            log.warn("entity can't be null");
            return null;
        }
        article.setCreate(new Date());
        article.setUpdate();
        return articleDAO.save(article);
    }

    @Override
    public void saveOrUpdate(IArticle<PK> article) {
        if (article == null) {
            log.warn("entity can't be null");
            return;
        }
        articleDAO.saveOrUpdate(article);
    }

    @Override
    public void remove(IArticle<PK> article) {
        if (article == null) {
            log.warn("entity can't be null");
            return;
        }
        articleDAO.remove(article.getId());
    }

    @Override
    public void remove(PK id) {
        if (id == null) {
            log.warn("id for entity can't be null");
            return;
        }
        articleDAO.remove(id);
    }

    @Override
    public IArticle<PK> findById(PK id) {
        if (id == null) {
            log.warn("id for entity can't be null");
            return null;
        }
        return articleDAO.findById(id);
    }

    @Override
    public Collection<IArticle<PK>> getAll() {
        return articleDAO.getAll();
    }

    @Override
    public Collection<IArticle<PK>> getAll(ILanguage lang) {
        if (lang == null) {
            log.warn("lang can't be null");
            return articleDAO.getAll();
        }
        return articleDAO.getAll(lang);
    }

    @Override
    public Collection<IArticle<PK>> getArticles(int start, int limit, String fieldName, boolean order) {
        return articleDAO.getEntityByParams(start, limit, fieldName, order);
    }

    @Override
    public Collection<IArticle<PK>> getArticles(ILanguage lang, int start, int limit, String fieldName, boolean order) {
        if (lang == null) {
            log.warn("language can't be null");
            articleDAO.getEntityByParams(start, limit, fieldName, order);
        }
        return articleDAO.getEntityByParams(start, limit, fieldName, order, lang);
    }

    @Override
    public int getCountArticles() {
        return articleDAO.getCountEntities();
    }

    @Override
    public int getCountArticles(ILanguage lang) {
        if (lang == null) {
            log.warn("language can't be null");
            articleDAO.getCountEntities();
        }
        return articleDAO.getCountEntities(lang);
    }

    @Override
    public Collection<IArticle<PK>> getLatestArticles() {
        log.debug("get latest article");
        return articleDAO.getEntityByParams(0, 10, "create", false);
    }

    @Override
    public Collection<IArticle<PK>> getLatestArticles(ILanguage lang) {
        log.debug("get latest article with language: {}", lang);
        if (lang == null) {
            log.debug("language can't be null");
            return articleDAO.getEntityByParams(0, 10, "create", false);
        }
        return articleDAO.getEntityByParams(0, 10, "create", false, lang);
    }
}
